package demo.gui;

import engine.core.scene.Scene;
import engine.gui.Action;
import engine.gui.GUIStyle;
import engine.gui.elements.Button;
import engine.gui.elements.Checkbox;
import engine.gui.elements.Label;
import engine.gui.elements.Panel;
import engine.gui.elements.RadioButtonGroup;
import engine.gui.elements.ScrollPane;
import engine.gui.elements.Slider;
import engine.gui.elements.TextField;
import engine.gui.popup.Popup;
import engine.sound.Musicplayer;

public class GUIScene extends Scene{
	
	public GUIScene(String name) {
		super(name);
	}

	@Override
	public void init() {
		initSound();
		
		setGUIStyle(GUIStyle.getDefaultStyle());
		
		final Panel p = new Panel(10, 10, 550, 400);
		p.setSolid(true);
		p.setAlign(Panel.ALIGN_RIGHT);
		p.setValign(Panel.ALIGN_BOTTOM);
		getWindowManager().addComponent(p);

		Label musicplayerLabel = new Label("Musicplayer:", 10, 10, 60, 30);
		p.addComponent(musicplayerLabel);
		
		Button previous = new Button("Previous", 10, 50, 95, 40);
		previous.setAction(new Action() {
			@Override
			public void performAction() {
				Musicplayer.getInstance().previousTrack();
			}
		});
		p.addComponent(previous);
		
		Button play = new Button("Play", 115, 50, 60, 40);
		play.setAction(new Action() {
			@Override
			public void performAction() {
				Musicplayer.getInstance().play();
			}
		});
		p.addComponent(play);
		
		Button pause = new Button("Pause", 185, 50, 75, 40);
		pause.setAction(new Action() {
			@Override
			public void performAction() {
				Musicplayer.getInstance().pause();
			}
		});
		p.addComponent(pause);
		
		Button stop = new Button("Stop", 270, 50, 60, 40);
		stop.setAction(new Action() {
			@Override
			public void performAction() {
				Musicplayer.getInstance().stop();
			}
		});
		p.addComponent(stop);
		
		Button next = new Button("Next", 340, 50, 60, 40);
		next.setAction(new Action() {
			@Override
			public void performAction() {
				Musicplayer.getInstance().nextTrack();
			}
		});
		p.addComponent(next);

		final Label label = new Label("1.0", 10, 125, 100, 20);
		p.addComponent(label);
		
		final Slider slider = new Slider(10, 100, 150, 20);
		slider.setMinValue(0);
		slider.setMaxValue(1);
		slider.setCurrentValue(1);
		slider.setAction(new Action() {
			@Override
			public void performAction() {
				label.setText(slider.getCurrentValue() + "");
				Musicplayer.getInstance().setVolume((float)slider.getCurrentValue());
			}
		});
		p.addComponent(slider);
		
		final Checkbox c = new Checkbox("Loop track", 170, 100, 20, 20);
		c.setAction(new Action() {
			@Override
			public void performAction() {
				Musicplayer.getInstance().setLoopTrack(c.isChecked());
			}
		});
		p.addComponent(c);
		
		Label l = new Label("Further GUI Elements:", 10, 155, 60, 20);
		p.addComponent(l);
		
		Button b = new Button("Create PopUp", 5, 185, 140, 40);
		b.setAction(new Action() {
			@Override
			public void performAction() {
				getWindowManager().addPopup(createPopup());
			}
		});
		p.addComponent(b);
		
		TextField f = new TextField(160, 190, 80, 30);
		p.addComponent(f);
		
		final RadioButtonGroup r = new RadioButtonGroup(10, 230, 20, 20);
		r.setAction(new Action() {
			@Override
			public void performAction() {
				System.out.println("selected index of radio button group: " + r.getSelectedIndex());
			}
		});
		r.addEntry("Entry 1");
		r.addEntry("Entry 2");
		p.addComponent(r);
		
		ScrollPane scrollPane = new ScrollPane(250, 155, 200, 200);
		scrollPane.setSolid(true);
		p.addComponent(scrollPane);
		
		Panel insidePanel = new Panel(0, 0, 300, 600);
		
		for(int i = 0; i < 10; i++) {
			Button aButton = new Button("Button " + (i + 1), 10, 10 + 50 * i, 100, 40);
			scrollPane.addComponent(aButton);
			
			TextField aTextfield = new TextField(120, 15 + 50 * i, 100, 30);
			insidePanel.addComponent(aTextfield);
		}
		
		scrollPane.addComponent(insidePanel);
	}
	
	private void initSound() {
		Musicplayer.getInstance().addTrack("sounds/demo/music/sphericalAgeTheme.ogg");
		Musicplayer.getInstance().addTrack("sounds/demo/music/amoTrailer.ogg");
	}

	private Popup createPopup() {
		final Popup popup = new Popup("Pop Up", (int)(Math.random() * getWindowManager().getWidth()), (int)(Math.random() * getWindowManager().getHeight()), 300, 150);
		
		Label l = new Label("This is a popup message", 5, 20, 300, 20);
		l.setAlignCenter(true);
		popup.addComponent(l);
		
		Button btn = new Button("Close", 100, 60, 100, 40);
		btn.setAction(new Action() {
			@Override
			public void performAction() {
				popup.dispose();
			}
		});
		popup.addComponent(btn);
		
		return popup;
	}
}
